package com.pipi.fanglan.timer;

import com.pipi.fanglan.cookie.CookieRepository;
import com.pipi.fanglan.dao.CommitOrderDao;
import com.pipi.fanglan.dao.OrderDao;
import com.pipi.fanglan.httpproxy.LoginProxy;
import com.pipi.fanglan.model.MyCookieStore;
import com.pipi.fanglan.model.WorkOrder;
import com.pipi.fanglan.model.form.ConfirmOrderForm;
import com.sun.jndi.toolkit.url.Uri;
import org.apache.http.client.CookieStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.List;

/**
 * Created by pipi on 2017/7/16.
 */
@Component
public class Scheduler {
    String hostUrl = "http://111.21.17.28:10001/partner";
    String domain = "111.21.17.28";

    @Autowired
    private CookieRepository cookieRepository;

    @Autowired
    public OrderDao orderDao;

    @Autowired
    public CommitOrderDao commitOrderDao;

    @Scheduled(cron="0 0/1 * * * ?") //每分钟执行一次
    public void statusCheck(){
        try{
            List<MyCookieStore> cookieStores = cookieRepository.findAll();
            if(cookieStores == null || cookieStores.size()==0){
                return;
            }
            for(MyCookieStore myCookieStore : cookieStores){
                CookieStore cookieStore = myCookieStore.getCookieStore();
                String result = LoginProxy.defautGet(hostUrl + "/sheet/pnrfaultdeal/pnrfaultdeal.do?method=showListsendundo",cookieStore);
                List<WorkOrder> orders =  orderDao.orderList(result);
                if(orders!=null && orders.size()>0){
                    for(WorkOrder order : orders){
                        System.out.println(order.getOrderTheme());
                        if(WorkOrder.NOT_COMMIT.equals(order.getOrderStatus())){
                            File fileText = new File("jiedan.txt");
                            FileWriter fileWriter = new FileWriter(fileText,true);
                            BufferedWriter bw = new BufferedWriter(fileWriter);
                            bw.write(order.getOrderTheme());
                            bw.newLine();
                            bw.close();
                            // TODO: 2017/7/16 接单
                            String datailUrl = hostUrl + "/sheet/pnrfaultdeal/" + order.getDetailUrl();
                            String detailHtml = LoginProxy.defautGet(datailUrl,cookieStore);
                            String getCommitFormHtmlUrl = commitOrderDao.getCommitFormUrl(detailHtml);
                            String commitFormHtml =  LoginProxy.defautGet(hostUrl + "/sheet/pnrfaultdeal/"+getCommitFormHtmlUrl,cookieStore);
                            try {
                                File fileText1 = new File("jiedan2.html");
                                FileWriter fileWriter1 = new FileWriter(fileText1);
                                fileWriter1.write(commitFormHtml);
                                fileWriter1.close();

                            }catch (Exception e){
                                e.printStackTrace();
                            }
                            ConfirmOrderForm orderForm = commitOrderDao.getCommitParams(commitFormHtml);

                            Uri uri = new Uri(datailUrl); // 为了获取ip
//                            String performClaimTaskUrl = uri.getScheme()+ "://" + uri.getHost() + ":" + uri.getPort() + orderForm.getPerformClaimTaskUrl();
//                            String preCommitResult = LoginProxy.defaultPost(performClaimTaskUrl,orderForm.getParams(),cookieStore);
//
//                            File preCommitText = new File("preCommitResult.txt");
//                            FileWriter preCommitfileWriter = new FileWriter(preCommitText);
//                            preCommitfileWriter.write(preCommitResult);
//                            preCommitfileWriter.close();
//
//                            String commitUrl = uri.getScheme()+ "://" + uri.getHost() + ":" + uri.getPort() + orderForm.getUrl();
//                            String commitResult = LoginProxy.defaultPost(commitUrl,orderForm.getParams(),cookieStore);
//
//                            File commitText = new File("commitResult.txt");
//                            FileWriter commitfileWriter = new FileWriter(commitText);
//                            commitfileWriter.write(commitResult);
//                            commitfileWriter.close();
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Scheduled(fixedRate=20000)
    public void testTasks() {
    }
}
